package org.galaxyworld.flycode.gui;

import org.apache.commons.lang3.StringUtils;
import org.galaxyworld.flycode.config.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class ConclusionPane extends AbstractMainPane {

    private JTextArea textArea;

    public ConclusionPane() {
        super("Conclusion");

        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                textArea.setText("");

                printJdbcConfig();
                printModelConfig();
                printServiceConfig();
                printAppConfig();

                firePropertyChange("last", null, this);
            }
        });
    }

    @Override
    protected void setupMainPane(JPanel mainPane) {
        mainPane.setLayout(new BorderLayout());

        textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        mainPane.add(scrollPane, BorderLayout.CENTER);

        textArea.setEditable(false);
    }

    private void printJdbcConfig() {
        JdbcConfigData jdbcConfigData = ConfigManager.getConfig(JdbcConfigData.class);
        textArea.append("JDBC Information\n--------------------------------------------------\n");
        textArea.append(String.format("Driver: %s\n", jdbcConfigData.getDriver()));
        textArea.append(String.format("URL: %s\n", jdbcConfigData.getUrl()));
        textArea.append(String.format("User Name: %s\n", jdbcConfigData.getUserName()));
        textArea.append(String.format("Password: %s\n", jdbcConfigData.getPassword()));
        textArea.append(String.format("Prefixes: %s\n", StringUtils.join(jdbcConfigData.getPrefixList(), ", ")));
        textArea.append(String.format("Include prefixes: %s\n", jdbcConfigData.isIncludePrefix() ? "Yes" : "No"));
        textArea.append("\n");
    }

    private void printModelConfig() {
        ModelConfigData modelConfigData = ConfigManager.getConfig(ModelConfigData.class);
        if (modelConfigData.isEnabled()) {
            textArea.append("Model Information\n--------------------------------------------------\n");
            textArea.append(String.format("Package: %s\n", modelConfigData.getPackageName()));
            textArea.append(String.format("Template: %s\n", modelConfigData.getTemplateName()));
            textArea.append("\n");
        }
    }

    private void printServiceConfig() {
        ServiceConfigData serviceConfigData = ConfigManager.getConfig(ServiceConfigData.class);
        if (serviceConfigData.isEnabled()) {
            textArea.append("Service Information\n--------------------------------------------------\n");
            textArea.append(String.format("Interface package: %s\n", serviceConfigData.getPackageName()));
            textArea.append(String.format("Implementation package: %s\n", serviceConfigData.getImplPackageName()));
            textArea.append(String.format("Template: %s\n", serviceConfigData.getTemplateName()));
            textArea.append("\n");
        }
    }

    private void printAppConfig() {
        AppConfigData appConfigData = ConfigManager.getConfig(AppConfigData.class);
        textArea.append("Application Information\n--------------------------------------------------\n");
        textArea.append(String.format("Output directory: %s\n", appConfigData.getOutputDirPath()));
        textArea.append("\n");
    }

}
